package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.service.YddLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {
    @Autowired
    private YddLinkService yddLinkService;

    //链接详情
    @GetMapping("/rowInfo")
    public Result rowInfo(String id) {
        YddLink yddLink = yddLinkService.getById(id);
        return Result.ok("", yddLink);
    }

    //链接修改
    @PostMapping("/update")
    public Result update(@RequestBody YddLink yddLink) {
        yddLinkService.updateById(yddLink);
        return Result.ok("修改成功", null);
    }

    //链接删除
    @GetMapping("/del")
    public Result del(String id) {
        yddLinkService.removeById(id);
        return Result.ok("删除成功", null);
    }

    //链接添加
    @PostMapping("/add")
    public Result add(@RequestBody YddLink yddLink) {
        yddLinkService.save(yddLink);
        return Result.ok("添加成功", null);
    }

    //链接列表
    @GetMapping("/list")
    public Result list(String type, Integer page, Integer limit) {
        //构造分页
        Page<YddLink> linkPage = new Page<>(page, limit);
        //构造条件,根据类型
        LambdaQueryWrapper<YddLink> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type != null, YddLink::getType, type);

        Page<YddLink> yddLinkPage = yddLinkService.page(linkPage, lqw);

        PageResult result = PageResult.answer(yddLinkPage.getTotal(), yddLinkPage.getSize(), yddLinkPage.getCurrent(), yddLinkPage.getRecords());

        return Result.ok("", result);
    }

}
